package com.lanhai.crowd.mvc.handler;

import com.github.pagehelper.PageInfo;
import com.lanhai.crowd.constant.CrowdConstant;
import com.lanhai.crowd.entity.Admin;
import com.lanhai.crowd.service.api.AdminService;
import com.lanhai.crowd.util.CrowdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
public class AdminController {

    @Autowired
    private AdminService adminService;

    @RequestMapping("/admin/do/login.html")
    public String doLogin(
            @RequestParam("login-user") String user,
            @RequestParam("login-pwd") String pwd,
            HttpSession session) {

        // 1.调用adminService.getLoginAdmin执行登录检查
        // 如果返回admin对象说明可以登陆，如果账户密码不对抛出异常
        Admin loginAdmin = adminService.getLoginAdmin(user, pwd);

        // 2.登录成功，将adminLogin存入session域中
        session.setAttribute(CrowdConstant.ATTR_NAME_LOGIN_ADMIN, loginAdmin);

        // 3.登陆成功后进入后台主页面
        return "redirect:/admin/to/main/page.html";
    }

    @RequestMapping("/admin/do/logout.html")
    public String doLogout(HttpSession session) {

        session.removeAttribute(CrowdConstant.ATTR_NAME_LOGIN_ADMIN);
        // 强制session失效
        session.invalidate();
        return "redirect:/admin/to/login/page.html";
    }


    @RequestMapping("/admin/get/page.html")
    public String getPage(@RequestParam(value = "keyword", defaultValue = "") String keyword,
                          @RequestParam(value = "pageNum", defaultValue = "1")Integer pageNum,
                          @RequestParam(value = "pageSize", defaultValue = "5")Integer pageSize,
                          ModelMap modelMap) {

        // 1.调用service方法获取PageInfo对象
        PageInfo<Admin> pageInfo = adminService.getPageInfo(keyword, pageNum, pageSize);

        // 2.将PageInfo对象存入modelMap中
        modelMap.addAttribute(CrowdConstant.ATTR_NAME_PAGE_INFO, pageInfo);

        return "admin-page";
    }


    @RequestMapping("/admin/remove/{adminId}/{pageNum}/{keyword}.html")
    public String remove(
            @PathVariable("adminId") Integer adminId,
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("keyword") String keyword) {

        // 执行删除
        adminService.remove(adminId);

        // 页面跳转,需要回到分页页面
        return "redirect:/admin/get/page.html?pageNum=" + pageNum
                + "&keyword=" + keyword;
    }


    @RequestMapping("/admin/save.html")
    public String save(Admin admin) {

        // 1.密码加密
        admin.setUserPwd(CrowdUtil.md5(admin.getUserPwd()));

        // 2.生成新增时间
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        admin.setCreateTime(format.format(date));


        adminService.saveAdmin(admin);

        // 新增用户后跳转到最后一页
        return "redirect:/admin/get/page.html?pageNum=" + Integer.MAX_VALUE;
    }

    @RequestMapping("/admin/to/edit/page.html")
    public String toEditPage(@RequestParam("adminId") Integer adminId,
                             ModelMap model) {

        // 1.根据id查找Admin
        Admin admin = adminService.getAdminById(adminId);

        // 2.将Admin存入Model
        model.addAttribute(CrowdConstant.ATTR_NAME_ADMIN, admin);

        return "admin-edit";
    }

    @RequestMapping("/admin/update.html")
    public String updateAdmin(Admin admin,
                              @RequestParam("pageNum") Integer pageNum,
                              @RequestParam("keyword") String keyword) {

        admin.setLoginAcct(null);
        adminService.updateAdmin(admin);
        return "redirect:/admin/get/page.html?pageNum=" + pageNum
                + "&keyword=" + keyword;
    }
}
